package com.gexiaobao.pigeon.ui.fragment.mine.organization.activity

import android.annotation.SuppressLint
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.webkit.WebView
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import androidx.media3.exoplayer.ExoPlayer
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.Constant
import com.gexiaobao.pigeon.app.model.bean.LiveAnnouncementResponse
import com.gexiaobao.pigeon.app.util.KvUtils
import com.gexiaobao.pigeon.app.util.StatusBarUtil.setColor
import com.gexiaobao.pigeon.databinding.ActivityLiveLevelContentBinding
import com.gexiaobao.pigeon.ui.fragment.mine.organization.FragmentInteractionChat
import com.gexiaobao.pigeon.ui.fragment.mine.organization.FragmentLivingInteraction
import com.gexiaobao.pigeon.viewmodel.OrgDetailViewModel
import com.gexiaobao.xpopup.util.KeyboardUtils
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.tabs.TabLayout
import com.xinbida.wukongim.WKIM
import com.xinbida.wukongim.entity.WKChannelType
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.LogUtils


/**
 * @Author      : hxw
 * @Date        : 2024/11/2 15:01
 * @Describe    : 直播关卡内容
 */
class ActivityLiveLevelContent : BaseActivity<OrgDetailViewModel, ActivityLiveLevelContentBinding>(), KeyboardUtils.OnSoftInputChangedListener {

    private var mRaceId = 0
    private var mOrgId = 0
    private var mRaceName = ""
    private var channelID = ""
    private var mChannelType: Byte = 0
    private var mLoginUID = ""
    private var mUid = ""
    private var token = ""
    private lateinit var dialogBottom: BottomSheetDialog

    @SuppressLint("SetTextI18n")
    override fun initView(savedInstanceState: Bundle?) {
        setColor(this, ContextCompat.getColor(this, R.color.color_99CCFF), 0)
        mDatabind.tvLiveInteractionTitle.text = "直播间"

        mRaceName = intent.getStringExtra("raceName").toString()

        mOrgId = intent.getIntExtra("orgId", 0)
        mRaceId = intent.getIntExtra("raceId", 0)
        token = intent.getStringExtra("token").toString()
        mLoginUID = KvUtils.decodeString(Constant.USERID)
        WKIM.getInstance().init(this, mLoginUID, token)
        channelID = intent.getStringExtra("channelId").toString()
        mUid = intent.getStringExtra("uid").toString()
        mChannelType = WKChannelType.GROUP//群聊

        mRaceId = intent.getIntExtra("raceId", 0)
        initTabLayout()
    }

    private fun initTabLayout() {
        mDatabind.tabLayout.tabRippleColor = ColorStateList.valueOf(ContextCompat.getColor(this, android.R.color.transparent))
        val tab1 = mDatabind.tabLayout.newTab().setText("直播")
        tab1.setTag("fragmentOrgPassList")
        mDatabind.tabLayout.addTab(tab1)
        val tab2 = mDatabind.tabLayout.newTab().setText("互动")
        mDatabind.tabLayout.addTab(tab2)

        mDatabind.tabLayout.addOnTabSelectedListener(object : TabLayout.BaseOnTabSelectedListener<TabLayout.Tab> {
            override fun onTabReselected(p0: TabLayout.Tab) {}
            override fun onTabUnselected(p0: TabLayout.Tab) {}

            override fun onTabSelected(p0: TabLayout.Tab) {
                replaceFragments(p0.position)
            }
        })
        replaceFragments(0)
    }

    private fun replaceFragments(position: Int) {
        supportFragmentManager.beginTransaction().apply {
            when (position) {
                0 -> replace(R.id.raceContainer, FragmentLivingInteraction.newInstance(mRaceId, mOrgId))
                1 -> replace(R.id.raceContainer, FragmentInteractionChat.newInstance(mOrgId, mRaceId, token, channelID, mUid))
            }
        }.commitAllowingStateLoss()
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.ivLiveBack, mDatabind.tvLiveDescribe) {
            when (it) {
                mDatabind.tvLiveDescribe -> announcementDetail()
                mDatabind.ivLiveBack -> finish()
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        /** 获取最新直播公告（app端）  */
        mViewModel.liveAnnouncementResult.observe(this) {
            showBottomDialog(it)
        }
    }

    private fun announcementDetail() {
        showLoading(getString(R.string.loading))
        mViewModel.getLatestLiveStreamingAnnouncement(mRaceId, mOrgId)
    }

    @SuppressLint("MissingInflatedId")
    private fun showBottomDialog(it: LiveAnnouncementResponse) {
        dialogBottom = BottomSheetDialog(this, R.style.BottomSheetDialog)
        val dialogView: View = LayoutInflater.from(this).inflate(R.layout.bottom_live_introdution, null)
        val close = dialogView.findViewById<AppCompatImageView>(R.id.ivDialogLiveClose)
        val webView = dialogView.findViewById<WebView>(R.id.webView)
        webView.loadDataWithBaseURL(null, it.body, "text/html", "utf-8", null)
        close.setOnClickListener {
            dialogBottom.dismiss()
        }
        dialogBottom.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialogBottom.setContentView(dialogView)
        dialogBottom.show()
    }

    override fun onSoftInputChanged(height: Int) {
        LogUtils.debugInfo(height.toString())
    }
}